<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <el-input v-model="form.head_goods_order_no" placeholder="头程单号" class="w-50"></el-input>
        <el-input v-model="form.head_box_order_no" placeholder="箱号" class="w-50"></el-input>
        <VGroup>
          <el-select style="width: 100px;" v-model="form.personRange.type" placeholder="请选择">
            <el-option v-for="item in personOptions" :key="item.value" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
          <select-personnel class="w-50" v-model="form.personRange.value"></select-personnel>
        </VGroup>
        <VGroup>
          <el-select style="width: 100px;" v-model="form.skuRange.type" placeholder="请选择">
            <el-option v-for="item in skuOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-input v-model="form.skuRange.value" placeholder="请输入" class="w-40"></el-input>
        </VGroup>

        <el-select class="selectClass" v-model="form.status" placeholder="状态" multiple collapse-tags clearable>
          <el-option v-for="item in options['head.headBoxOrder']?.[0].headBoxOrderStatus" :key="item.value"
            :label="item.label" :value="item.value"></el-option>
        </el-select>

        <el-select class="selectClass" v-model="form.sync_status" placeholder="同步状态" multiple collapse-tags clearable>
          <el-option v-for="item in options['head.headBoxOrder']?.[0].headBoxOrderSyncStatus" :key="item.value"
            :label="item.label" :value="item.value"></el-option>
        </el-select>
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 100px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()" style="max-width: 300px" />
        </VGroup>

      </template>
      <template #toolbar_btns>
        <el-button class="ml-auto" @click="handleExport"><svg-icon icon-class="import" />导出</el-button>
      </template>
    </VTable>
    <ComModal ref="ComModalRef" :wareDataList="wareDataList" @query="query"></ComModal>
    <VLog ref="logRef" class="dialogPublic" type="distribution" :api="getHeadLog"></VLog>
    <derived-center ref="DerivedCenterRef"></derived-center>

  </VPage>
</template>
<script setup name="HeadPacking-list" lang="jsx">
import { ElMessage, ElMessageBox } from 'element-plus'
import { nextTick, ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { queryHeadList, cancelHeadOrder, getHeadLog } from './api'
import ComModal from './components/ComModal.vue'
import VLog from '@/components/VLog';

const timeOption = [
  // { label: "创建时间", value: "create_time" },
  { label: "装箱时间", value: "create_time" },
]

const personOptions = [
  { label: "创建人", value: "create_user_id" },
  { label: "装箱人", value: "out_warehouse_user_id" },
  { label: "修改人", value: "update_user_id" },
]

const skuOptions = [
  { label: "SKU", value: 'sku' },
  { label: "FNSKU", value: 'to_fn_sku' },
]


const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 60 },
  formConfig: {
    data: {
      shop_id: '',
      create_time: '',
      status: '',
      operations_personnel: '',
      skuRange: { type: 'sku', value: [], range: true },
      personRange: { type: 'create_user_id', value: [], range: true },
      timeRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    {
      type: "seq", title: '序号', width: 80
    },
    {
      field: 'head_box_order_no', title: '箱号', width: 210
    },
    {
      field: 'head_goods_order_no', title: '头程单号', width: 180
    },
    {
      field: 'shipment_id', title: 'Shipment ID', width: 180
    },
    {
      field: 'sku_num', title: 'SKU数量', width: 120
    },
    {
      field: 'sku_count', title: 'SKU件数', width: 120
    },
    {
      field: 'status_cn', title: '状态', width: 120
    },
    {
      field: 'weight', title: '重量（g）', width: 120
    },
    {
      field: 'length', title: '长（cm）', width: 120
    },
    {
      field: 'width', title: '宽（cm）', width: 120
    },
    {
      field: 'height', title: '高（cm）', width: 120
    },
    {
      field: 'throw_weight', title: '抛重', width: 120
    },
    {
      field: 'throw_weight_proportion', title: '抛重比', width: 120
    },
    {
      field: 'create_user', title: '装箱人', width: 140
    },
    {
      field: 'create_time', title: '装箱时间', width: 180
    },
    {
      field: 'update_user', title: '最近修改人', width: 120
    },
    {
      field: 'update_time', title: '最近修改时间', width: 120
    },
    {
      title: '操作', width: "185", fixed: "right", slots: {
        default: ({ row }) => {
          return <div class="table-option">
            <div>
              <el-link type="primary" underline={false} class="mr-2" onClick={() => handleCancellation(row.id)}>作废</el-link>
              <el-link type="primary" underline={false} class="mr-2" onClick={() => open('edit', row.id)}>修改</el-link>
              <el-link type="primary" underline={false} class="mr-2" onClick={() => open('view', row.id)}>查看</el-link>
              <el-link type="primary" underline={false} class="mr-2" onClick={() => viewLog(row.id)}>操作记录</el-link>
            </div>
          </div >
        }
      }
    }
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await queryHeadList({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: [],
  wareDataList: [],
  floorList: [],
  areaList: []
})
const { options, wareDataList, floorList, areaList } = toRefs(data)

// 获取枚举类
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "head.headBoxOrder"
    ]
  }).then(res => {
    options.value = res.data
  })
}
provide('options', options)
fetchOption()


const ComModalRef = ref()
const open = (type, id) => {
  ComModalRef.value.open(type, id)
}

const handleCancellation = (id) => {
  ElMessageBox.confirm('是否作废当前项?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    cancelHeadOrder({ ids: [id] })
      .then((res) => {
        query()
        ElMessage.success(res.message)
      })
  })
}


// 导出
const DerivedCenterRef = ref()
const handleExport = (val) => {
  let condition = tableRef.value.getQueryForm()
  DerivedCenterRef.value.open({
    id: 49,
    tag_name: 'wms',
    condition
  })
}

//查看日志
const logRef = ref()
const viewLog = (id) => {
  logRef.value.open(id)
}
</script>